package com.midian.common.interceptor;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.log4j.Logger;

import com.midian.action.mc.banner.BannerAction;
import com.midian.common.annotation.InfoFlexIds;
import com.midian.model.banner.Banner;
import com.midian.model.common.vo.InfoFlexVo;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
import com.opensymphony.xwork2.interceptor.PreResultListener;

/**
 * 信息窗口内嵌flexigrid处理拦截器
 * @author Savage.M
 *
 */
public class InfoFlexInterceptor extends MethodFilterInterceptor {
	
	private static final Logger LOG = Logger.getLogger(InfoFlexInterceptor.class);
	/*private static List<InfoFlexVo> LIST = null ;
	static{
		
		LIST = new ArrayList<>();
		
		List<String> list = new ArrayList<>();
		
		for(int i = 0 ; i < 10 ; i++){
			list.add("user");
		}
		
		LIST.add(new InfoFlexVo("yonghu", list));
		
	}*/
	
	
	/**
	 * 处理属性Map
	 * @param infoFlexIds 信息窗口内嵌flexigrid的id集合注解对象
	 * @param propMap 属性Map
	 * @param fieldName 属性名
	 */
	private void opPropMap(InfoFlexIds infoFlexIds, Map<String, String> propMap, String fieldName) {
		String hidName = infoFlexIds.name();
		//如果没有指定隐藏域名称，使用变量名作为名称
		if("".equals(hidName)) {
			hidName = fieldName;
		}
		propMap.put(fieldName, hidName);
	}

	@Override
	protected String doIntercept(ActionInvocation invocation) throws Exception {
		invocation.addPreResultListener(new PreResultListener() {
			@Override
			public void beforeResult(ActionInvocation invocation, String arg1) {
				
				//获取目标Action对象
				Object action = invocation.getAction();
				Field[] fields = action.getClass().getDeclaredFields();
				Method[] methods = action.getClass().getDeclaredMethods();
				//属性Map，key为属性名，val为checkbox的name值
				Map<String, String> propMap = new LinkedHashMap<>();
				for (Field field : fields) {
					if(field.isAnnotationPresent(InfoFlexIds.class)) {
						InfoFlexIds infoFlexIds = field.getAnnotation(InfoFlexIds.class);
						String fieldName = field.getName();
						opPropMap(infoFlexIds, propMap, fieldName);
					}
				}
				for(Method method: methods) {
					String methodName = method.getName();
					if(method.getName().startsWith("get") && method.isAnnotationPresent(InfoFlexIds.class)) {
						InfoFlexIds infoFlexIds = method.getAnnotation(InfoFlexIds.class);
						String fieldName = methodName.substring(3, 4).toLowerCase() + methodName.substring(4);
						opPropMap(infoFlexIds, propMap, fieldName);
					}
				}
				List<InfoFlexVo> infoFlexVoList = new ArrayList<>();
				for(Entry<String, String> entry : propMap.entrySet()) {
					String fieldName = entry.getKey(); //属性名
					String hidName = entry.getValue(); //隐藏域名称
					String getMethodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
					try {
						Object fieldVal = action.getClass().getMethod(getMethodName).invoke(action);
						
						if(fieldVal instanceof String[]) {
							infoFlexVoList.add(new InfoFlexVo(hidName, Arrays.asList((String[]) fieldVal)));
						} else if(fieldVal instanceof List) {
							infoFlexVoList.add(new InfoFlexVo(hidName, (List<String>) fieldVal));
						}
					} catch (Exception e) {
						LOG.error(e.getMessage());
					}
				}
				
				try {
					action.getClass().getMethod("setInfoFlexVoList", List.class).invoke(action, infoFlexVoList);
					//action.getClass().getMethod("setInfoFlexVoList", List.class).invoke(action, LIST);
				} catch(Exception e) {
					LOG.error(e.getMessage());
				}
			}
		});
		return invocation.invoke();
	}
}
